﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Domain.ActiveDirectory.Model;
using Domain.ActiveDirectory.Accessor;
using Domain.ActiveDirectory.Mutator;

namespace Domain.ActiveDirectory.Provider
{
    public interface IActiveDirectoryDataProvider
    {
        IEnumerable<User> GetUsersByTitle(params string[] titleSubstrings);
        IEnumerable<User> GetUsersByUsername(params string[] usernameSubstrings);
        IEnumerable<User> GetUsersByCompleteName(params string[] completeNameSubstrings);
        IEnumerable<Group> GetGroupsByName(params string[] groupNameSubstrings);
        void AddUserToGroup(Group group, User user);
        void RemoveUserFromGroup(Group group, User user);
        void MoveUserFromGroupToGroup(Group fromGroup, Group toGroup, User user);
    }

    public class ActiveDirectoryDataProvider : IActiveDirectoryDataProvider
    {
        private UserAccessor _userAccessor;
        private GroupAccessor _groupAccessor;

        //private UserMutator _userMutator;
        private GroupMutator _groupMutator;

        public ActiveDirectoryDataProvider()
        {
            Connection connection = new Connection();
            _userAccessor = new UserAccessor(connection);
            _groupAccessor = new GroupAccessor(connection);
            _groupMutator = new GroupMutator(connection);
        }

        public IEnumerable<User> GetUsersByTitle(params string[] titleSubstrings)
        {
            List<string> toIEnumerable = new List<string>();
            foreach (string username in titleSubstrings)
            {
                toIEnumerable.Add(username);
            }
            return _userAccessor.GetUsersByTitle(toIEnumerable);
        }

        public IEnumerable<User> GetUsersByUsername(params string[] usernameSubstrings)
        {
            List<string> toIEnumerable = new List<string>();
            foreach (string username in usernameSubstrings)
            {
                toIEnumerable.Add(username);
            }
            return _userAccessor.GetUsersByUsername(toIEnumerable);
        }

        public IEnumerable<User> GetUsersByCompleteName(params string[] completeNameSubstrings)
        {
            List<string> toIEnumerable = new List<string>();
            foreach (string completeName in completeNameSubstrings)
            {
                toIEnumerable.Add(completeName);
            }
            return _userAccessor.GetUsersByCompleteName(toIEnumerable);
        }

        public IEnumerable<Group> GetGroupsByName(params string[] groupNameSubstrings)
        {
            List<string> toIEnumerable = new List<string>();
            foreach (string groupName in groupNameSubstrings)
            {
                toIEnumerable.Add(groupName);
            }
            return _groupAccessor.GetGroupsByName(toIEnumerable.AsEnumerable());
        }

        public void AddUserToGroup(Group group, User user)
        {
            if (group != null && user != null)
                _groupMutator.AddUser(group.DistinguishedName, user.DistinguishedName);
        }

        public void RemoveUserFromGroup(Group group, User user)
        {
            if (group != null && user != null)
                _groupMutator.RemoveUser(group.DistinguishedName, user.DistinguishedName);
        }

        public void MoveUserFromGroupToGroup(Group fromGroup, Group toGroup, User user)
        {
            if (fromGroup != null && toGroup != null && user != null)
                _groupMutator.MoveUser(fromGroup.DistinguishedName, toGroup.DistinguishedName, user.DistinguishedName);
        }
    }
}
